﻿
using SQLiteDB;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utils.Common;
using Utils.Common.Enum;
using ZK.BLL;
using ZK.BLL.Config;
using ZK.BLL.MainExecute;
using ZK.Client.WinForm;
using ZK.Entity;
using ZK.MyControl;

namespace ZK.Client
{
    public partial class FrmDefault : Form
    {
        public FrmDefault()
        {
            InitializeComponent();
            FromInit();
           // this.WindowState = FormWindowState.Maximized;
           
        }

    
        private void FrmDefault_Load(object sender, EventArgs e)
        {

            if (Class_Config.PrinetType == "WDJ"|| Class_Config.PrinetType == "VDJ")
            {
              
                FormHelper.LoadForm(typeof(FrmMainExecute));
            }
            else
            {

                FormHelper.LoadForm(typeof(FrmCIJExecute));
            }


            LogHelper.WriteInfo("登录系统");
 

            lblStation.Text = Class_Config.Station;


            SystemConfig.GetConfig().SystemConfigEditHandler += systemConfigEditHandler;
            lblTitle.Text = AppSettingsConfig.Title;



            SQLiteORM.Instance.CreateTable(new PrintData());
            SQLiteORM.Instance.CreateTable(new ProductInfo());
            SQLiteORM.Instance.CreateTable(new CodeRule());
            SQLiteORM.Instance.CreateTable(new Seqence());
            SQLiteORM.Instance.CreateTable(new UserInfo());
            SQLiteORM.Instance.CreateTable(new CIJTemplate());
            SQLiteORM.Instance.CreateTable(new VDJTemplate());

            SQLiteORM.Instance.AddColumn("PrintData", "PatentNO");

            lblUserInfo.Text = $"当前用户：{ Class_Config.UserInfo.PersonName}({ Class_Config.UserInfo.PrintName})";
        }

        void systemConfigEditHandler(object sender ,EventArgs e)
        {
 
        }


 

        /// <summary>
        ///  初始化
        /// </summary>
        private void FromInit()
        {
            List<Type> types = new List<Type>();
            List<Button> buttons = new List<Button>();
            if (Class_Config.PrinetType == "WDJ"|| Class_Config.PrinetType == "VDJ")
            {
                types.Add(typeof(FrmMainExecute));
                types.Add(typeof(FrmPrintData));
                types.Add(typeof(FrmProductInfo));
                types.Add(typeof(FrmVDJTemplate));
                types.Add(typeof(FrmUserInfo));
                types.Add(typeof(FrmSystemSet));
                buttons.Add(this.AddButton("运行界面"));
                buttons.Add(this.AddButton("喷码数据"));
                buttons.Add(this.AddButton("产品型号"));
                buttons.Add(this.AddButton("模板设置"));
                buttons.Add(this.AddButton("用户信息"));
                buttons.Add(this.AddButton("系统设置"));
                FormHelper.Init(buttons, this.panelForm, types);
              
            }
            else {
                types.Add(typeof(FrmCIJExecute));
                types.Add(typeof(FrmPrintData));
                types.Add(typeof(FrmProductInfo));
                types.Add(typeof(FrmCIJTemplate));
                types.Add(typeof(FrmUserInfo));
                types.Add(typeof(FrmCIJSystemSet));
                buttons.Add(this.AddButton("运行界面"));
                buttons.Add(this.AddButton("喷码数据"));
                buttons.Add(this.AddButton("产品型号"));
                buttons.Add(this.AddButton("模板设置"));
                buttons.Add(this.AddButton("用户信息"));
                buttons.Add(this.AddButton("系统设置"));

                FormHelper.Init(buttons, this.panelForm, types);
            
            }
   
         
        }

        private ButtonEx AddButton(string text)
        {
            ButtonEx btn = new ButtonEx();
            btn.Size = new Size(90, 37);
            btn.Text = text;
            btn.Dock = DockStyle.Right;
            btn.Font = new Font("宋体", 12, FontStyle.Bold);
            btn.ForeColor = Color.White;
            btn.DrawBorder = false;
            btn.BoolDrawGlass = false;
            btn.Radius = 20;
            btn.RoundStyle = RoundStyle.Top;
            panelButton.Controls.Add(btn);
            btn.BaseColor = Color.FromArgb(40, 132, 194);
            btn.BackColor= Color.FromArgb(2, 97, 187); 
            //btn.BringToFront();
            return btn;
        }
 

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.Visible == false)
                this.Show();
        }



        private void 显示界面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.Visible == false)
                this.Show();
        }

        private void 退出程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            FormClose();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {

            //if (GlobalExecute.Instance.ExecuteInfo1st.IsExecuting|| GlobalExecute.Instance.ExecuteInfo2nd.IsExecuting)
            //{
            //    MessageBox.Show("必须先停止运行程序!");
            //    return;
            //}
            if (MessageBox.Show("确定退出?","提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)== DialogResult.Yes)
            {
                LogHelper.WriteInfo("退出系统");
                FormClose();
                Process.GetCurrentProcess().Kill();
            }
        }

        private void FormClose()
        {

            this.Close();
        }

        private void btnMini_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        
        private void btnSystem_Click(object sender, EventArgs e)
        {
            FrmPassword frmPassword = new FrmPassword();
            if (frmPassword.ShowDialog() == DialogResult.OK)
            {
                FrmSystemSet frmSystemSet = new FrmSystemSet();
                frmSystemSet.ShowDialog();
            }
        }

        private void 喷码机设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.Visible == false)
            {
                this.Show();
            }
        }

        private void 退出程序ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                LogHelper.WriteInfo("退出系统");
                FormClose();
                Process.GetCurrentProcess().Kill();
            }
        }

        private void 喷码机设置ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FrmPrinter frm = new FrmPrinter();
            frm.ShowDialog();

        }
    }
}